<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #div1{
            width:200px;
            height:200px;
            border: 1px solid #ccc;
            text-align: center;
            line-height: 200px;
        }
    </style>
</head>
<body>
    <div id="div1">111</div>

    <input type="button" value="测试" id="btn1">
</body>
</html>
<script>
    debugger;
    var addEvent = (function(){
        if(window.addEventListener){
            return function(elem,type,handler){
                    elem.addEventListener(type,handler,false);
            }
        }else{
            return function(elem,type,handler){
                elem.attachEvent("on"+type,handler);
            }
        }  
    })();


    addEvent(document.getElementById("div1"),"click",function(){
        alert("1111");
    });

    addEvent(document.getElementById("btn1"),"click",function(){
        alert("2222");
    });


    var addEvent = function(elem,type,handelr){
        if(window.addEventListener){
           addEvent =  function(elem,type,handler){
                elem.addEventListener(type,handler,false);
            }
        }else{
           addEvent = function(elem,type,handler){
               elem.attachEvent("on" +type,handler);
           }
        }
        addEvent(elem,type,handler);
    }
</script>